package com.mseiferle.mediabrowser.client;

import java.util.HashMap;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Htmlmediabrowser implements EntryPoint {

	/**
	 * @param i the Image Dom to be set.
	 * @param url the source for the url.
	 */
	public static void setUrl(final Image i, final String url) {
		i.setUrl(url.replaceAll("\"", ""));
	}

	/**
	 * @param container the element to be filled.
	 * @param html the content for the element.
	 */
	public static void setHTML(final HTML container, final String html) {
		container.setHTML(html);
	}

	/**
	 * _TEMP_ urls for testing.
	 */
	// private MMedia[] urls = null;
	private HashMap<Integer, String> urls = new HashMap<Integer, String>();

	/**
	 * Current state of the Application.
	 */

	private HTML currentState = new HTML();

	/**
	 * The applications mainPanel.
	 */
	private DockPanel mp = new DockPanel();

	/**
	 * next navigation.
	 */
	private Button next = new Button("Weiter >");
	/**
	 * back navigation.
	 */
	private Button prev;

	/**
	 * The content panel.
	 */
	private Image contents = new Image();

	/**
	 * Counter for iamge urls.
	 */

	@SuppressWarnings("unused")
	private JSONArray jsonData;

	/**
	 * just for counting.
	 */
	private int i;

	/**
	 * json data already fetched.
	 */
	@SuppressWarnings("unused")
	private boolean jsonLoaded;
	/**
	 * This is the entry point method.
	 */

	public final void onModuleLoad() {
		// Allow 4 pixels of spacing between each cell
		this.init();

		mp.setSpacing(Constants.SPACING);
		mp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);

		prev = new Button("< Zurück");
		prev.setStylePrimaryName("disabled");
		next.setStylePrimaryName("disabled");
		// Add text widgets all around
		mp.add(new HTML("<h1>Client Line 1: mehr mehr<h1>"), DockPanel.NORTH);
		mp.add(new HTML("<h2>Client Line 2: weitere Infos zum Kunden</h2>"),
				DockPanel.NORTH);
		currentState = new HTML("Loading Images...");
		mp.add(currentState, DockPanel.SOUTH);

		mp.add(next, DockPanel.EAST);
		mp.setCellVerticalAlignment(next, HasVerticalAlignment.ALIGN_BOTTOM);
		mp.add(prev, DockPanel.WEST);
		mp.setCellVerticalAlignment(prev, HasVerticalAlignment.ALIGN_BOTTOM);


		prev.addClickHandler(new ClickHandler() {
			public void onClick(final ClickEvent event) {
				if (--i < 0) {
					i = urls.size() - 1;
				}
				Htmlmediabrowser.setUrl(contents, urls.get(i));
				Htmlmediabrowser.setHTML(currentState, "Bild " 
						+ (i + 1) + " von " + urls.size());
			}
		});
		next.addClickHandler(new ClickHandler() {
			public void onClick(final ClickEvent event) {
				if (++i >= urls.size()) {
					i = 0;
				}
				Htmlmediabrowser.setUrl(contents, urls.get(i));
				Htmlmediabrowser.setHTML(currentState, "Bild " 
						+ (i + 1) + " von " + urls.size());
			}
		});
		// Add scrollable text in the center
		ScrollPanel scroller = new ScrollPanel(contents);
		scroller.setSize("800px", "400px");
		mp.add(scroller, DockPanel.CENTER);
		RootPanel.get().add(mp);

	}

	/**
	 * .
	 */
	public final void init() {
		new GetJSONValue(this, "http://localhost:8080/json.js");
		urls = new HashMap<Integer, String>(Constants.INITLOAD,
				Constants.LOADFACTOR);
	}

	/**
	 * @param v
	 *            a.
	 */
	public final void setJSONValue(final JSONArray v) {
		this.jsonData = v;
		JSONObject a;
		int j = 0;
		for (j = 0; j < v.size(); j++) {
			a = v.get(j).isObject();

			if (a == null) {
				continue;
			}
			urls.put(new Integer(j), a.get("src").toString());
			Htmlmediabrowser.setUrl(contents, urls.get(0));
			Htmlmediabrowser.setHTML(currentState, "Bild " 
					+ (i + 1) + " von " + urls.size());
			prev.setStylePrimaryName("gwt-Button");
			next.setStylePrimaryName("gwt-Button");

		}
	}
}
